package com.rongzhe.house.manager;

import com.rongzhe.house.entity.GetSignTo;
import com.rongzhe.house.entity.PayContstract;
import com.rongzhe.house.entity.ResponseEntityVo;
import com.rongzhe.house.entity.voo.Goods;
import com.rongzhe.house.entity.voo.HouseDetail;
import com.rongzhe.house.entity.voo.OrderVo;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.internet.InternetRequestWorker;
import com.rongzhe.house.internet.ResponseDispatcher;
import com.rongzhe.house.internet.net.OrderInterface;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by huang on 2018/1/4.
 */

public class OrderManager {
    public enum PayWay {
        WEIXIN, ALI
    }

    private PayWay mPayWay;
    private Goods mCurrentGoods;
    private String mOrderId;

    public PayWay getPayWay() {
        return mPayWay;
    }

    public void setPayWay(PayWay payWay) {
        this.mPayWay = payWay;
    }

    public String getOrderId() {
        return mOrderId;
    }

    public Goods getCurrentGoods() {
        return mCurrentGoods;
    }

    private OrderManager() {
    }

    private static OrderManager sInstance;

    public static OrderManager getInstance() {
        if (sInstance == null) {
            sInstance = new OrderManager();
        }
        return sInstance;
    }

    private HouseDetail mOrderHouseDetail;
    private String mPayType;

    public void setPayType(String payType) {
        mPayType = payType;
    }

    public String getPayTypeId() {
        return mPayType;
    }

    public String getPayType() {
        return DictionaryManager.getInstance()
                .pareItem(DictionaryManager.DICTIONARY_PAY_TYPE_OPTION, mPayType);
    }

    public HouseDetail getOrderHouseDetail() {
        return mOrderHouseDetail;
    }

    public void setOrderHouse(HouseDetail houseDetail) {
        mOrderHouseDetail = houseDetail;
    }

    public void getGoods(String houseId, DataListener dataListener) {
        InternetRequestWorker.getInstance().asyncNetwork(InternetRequestWorker.getInstance()
                        .create(OrderInterface.class).getGoods(houseId),
                new ResponseDispatcher(dataListener) {
                    @Override
                    protected void handleSuccess(ResponseEntityVo data) {
                        if (data.success() && data.getData() != null) {
                            List<Goods> datas = (List<Goods>) data.getData();
                            if (datas.size() > 0) {
                                mCurrentGoods = datas.get(0);
                            }
                        }
                    }
                });
    }

    public void createOrder(String goodsId, DataListener dataListener) {
        Map<String, String> data = new HashMap<>(1);
        data.put("itemId", goodsId);
        InternetRequestWorker.getInstance().asyncNetwork(InternetRequestWorker.getInstance()
                        .create(OrderInterface.class).createOrder(data),
                new ResponseDispatcher(dataListener) {
                    @Override
                    protected void handleSuccess(ResponseEntityVo data) {
                        if (data.success() && data.getData() != null) {
                            String orderId = (String) data.getData();
                            mOrderId = orderId;
                        }
                    }
                });
    }

    public void getAliSign(String orderId, DataListener<PayContstract> dataListener) {
        GetSignTo signTo = new GetSignTo(orderId, 2);
        InternetRequestWorker.getInstance().asyncNetwork(InternetRequestWorker.getInstance()
                        .create(OrderInterface.class).getOrderSign(signTo),
                new ResponseDispatcher(dataListener));
    }

    public void getOrderList(boolean finished, DataListener<List<OrderVo>> dataListener) {
        InternetRequestWorker.getInstance().asyncNetwork(InternetRequestWorker.getInstance()
                        .create(OrderInterface.class).getOrderList(finished ? 2 : 1),
                new ResponseDispatcher(dataListener));
    }
}
